package cst.a000wxb.fragment115wxb.model;

import java.util.List;

import cst.a000wxb.fragment115wxb.bean.ArticleBean;
import cst.a000wxb.fragment115wxb.iface.ArticleIface;
import cst.a000wxb.fragment115wxb.iface.ArticleListener;
import cst.a000wxb.fragment115wxb.service.ArticleService;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by Bobx on 2017/5/26.
 */

public class ArticleModel implements ArticleIface {
    private Retrofit retrofit;
    private static String RASE_URL="http://amicool.neusoft.edu.cn/";

    public  ArticleModel(){
        retrofit = new Retrofit.Builder()
                .baseUrl(RASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();

    }
    @Override
    public void getResultList(String mod, int page, String sessionID, final ArticleListener listener) {
        ArticleService articleService= retrofit.create(ArticleService.class);
        Call<List<ArticleBean>> call = articleService.getResult(mod, page, sessionID);
        call.enqueue(new Callback<List<ArticleBean>>() {
            @Override
            public void onResponse(Call<List<ArticleBean>> call, Response<List<ArticleBean>> response) {
                if(response.isSuccessful()&&response.body()!=null){
                    listener.onResponse(response.body());
                }
            }

            @Override
            public void onFailure(Call<List<ArticleBean>> call, Throwable t) {
                listener.onFail(t.toString());

            }
        });

    }
}

